package buscador.implementation;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class Indexer {

	public Indexer(){	
	}
	
	public void createIndex(){
		try{
			Directory concertsDir = FSDirectory.open(new File(System.getProperty("user.dir")+File.separator+"conciertos_index"));
			Directory restaurantsDir = FSDirectory.open(new File(System.getProperty("user.dir")+File.separator+"restaurantes_index"));
			//Directory ofertasDir = FSDirectory.open(new File(System.getProperty("user.dir")+File.separator+"offers_index"));
			//Directory theatreDir = FSDirectory.open(new File(System.getProperty("user.dir")+File.separator+"teatro_index"));
			//Directory sportsDir = FSDirectory.open(new File(System.getProperty("user.dir")+File.separator+"desportes_index"));
			//Directory showsDir  = FSDirectory.open(new File(System.getProperty("user.dir")+File.separator+"espectaculos_index"));
			
			Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
			IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_44, analyzer);
	
			iwc.setOpenMode(OpenMode.CREATE);
			
			Class.forName("org.postgresql.Driver");
			Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/bizigasteiz", "postgres", "postgres");
			
			//Indice para restaurantes
			IndexWriter writer = new IndexWriter(restaurantsDir, iwc);
			addRestaurantsIndex(writer, conn);
			writer.close();
			//Indice para conciertos
			writer = new IndexWriter(concertsDir, iwc);
			addConcertsIndex(writer, conn);
			writer.close();
		}
		catch(Exception o){
			o.printStackTrace();
		}
	}
	
	public void addRestaurantsIndex(IndexWriter writer, Connection conn){
		try {
            String query = "select * from restaurantes";
            PreparedStatement pstmt = conn.prepareStatement(query);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
            	Document doc = new Document();
            	doc.add(new TextField("nombre", rs.getString("nombre"), TextField.Store.YES));
            	doc.add(new TextField("descripcion", rs.getString("descripcion"), TextField.Store.YES));
            	doc.add(new TextField("descripcion_corta", rs.getString("descripcion_corta"), TextField.Store.YES));
            	doc.add(new TextField("descripcion_normal", rs.getString("descripcion_normal"), TextField.Store.YES));

            	writer.addDocument(doc);
            }
        } catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void addConcertsIndex(IndexWriter writer, Connection conn){
		try { 
            String query = "select * from conciertos";
            PreparedStatement pstmt = conn.prepareStatement(query);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
            	Document doc = new Document();
            	doc.add(new IntField("cod", rs.getInt("cod"), IntField.Store.YES));
            	doc.add(new TextField("nombre", rs.getString("nombre"), TextField.Store.YES));
            	doc.add(new TextField("descripcion_normal", rs.getString("descripcion_normal"), TextField.Store.YES));
            	doc.add(new TextField("descripcion_corta", rs.getString("descripcion_corta"), TextField.Store.YES));
            	doc.add(new TextField("descripcion_larga", rs.getString("descripcion_larga"), TextField.Store.YES));

            	writer.addDocument(doc);
            }
        } catch (Exception e) {
			e.printStackTrace();
		}
	}
}
